define(["jquery","bbcare"],function($,$bb){
	var servers = {};
	var URL = $bb.baseUrl("blood");

    /**
     * 患者治疗计划查询
     */
    servers.ajaxPtDisPlanInfo = function(datas,callback) {
        $bb.ajax({
            url: URL + "ptDisPlan/getPtDisPlanInfo/"+ datas.info.userId +"/"+ datas.info.queueId,
            data: JSON.stringify({
                data: {
                	"userId":datas.info.userId,
                	"queueId":datas.info.queueId
                }
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,datas);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "患者治疗计划查询出错！");
                }
            }
        });
    }

    /**
     * 获取当前阶段
     */
    servers.getCurrStage = function(stages,ptStageId){
    	for (var i = 0; i < stages.length; i++) {
    		if( stages[i].ptStageId == ptStageId ){
    			return stages[i];
    		}
    	}
    }

    /**
     * 获取当前阶段索引
     */
    servers.getCurrStageIndex = function(stages){
        for (var i = 0; i < stages.length; i++) {
            if( stages[i].ptDisCoursePOs && stages[i].ptDisCoursePOs.length > 0 ){
                for (var j = 0; j < stages[i].ptDisCoursePOs.length; j++) {
                    if( stages[i].ptDisCoursePOs[j].isCurrCourse ){
                        return {
                            "currStageIndex":i,
                            "currCourseIndex":j,
                        }
                    }
                }
            }
        }
        return {
            "currStageIndex":0,
            "currCourseIndex":0,
        }
    }

    /**
     * 修改患者身高、体重、体表面积
     */
    servers.ajaxChangeHeightAndWeight = function(params,callback,callback2,datas) {
        $bb.ajax({
            url: URL + "ptEhrCourse/changeHeightAndWeight",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,datas);
                } else {
                    callback2(data);
                }
            }
        });
    }

    /**
     * 专科袋药品表头信息查询
     */
    servers.ajaxMedicineInfo = function(params,callback) {
        $bb.ajax({
            url: URL + "specialistBag/medicine/getMedicineInfo",
            data: JSON.stringify({
                data: {
                	"busiId":params.currCourse.ptCourseId,
					"userId":params.currScheme.userId,
                }
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,params);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "查询药品信息出错！");
                }
            }
        });
    }

    /**
     * 专科袋疗程药品剂量查询
     */
    servers.ajaxListPtCourseMedDose = function(params,callback,datas) {
        $bb.ajax({
            url: URL + "specialistBag/medicine/listPtCourseBagInfo",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,datas);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "查询药品剂量出错！");
                }
            }
        });
    }

    /**
     * 处理table数据
     */
    servers.handleTable = function(opt){
        var nowDate = $bb.simple.getCurTime().date;
        var reg = /^(\d+)(\.\d{1,2})?$/;
        for (var i = 0; i < opt.days.length; i++) {
            // 为了和表头里的药品一一对应
            opt.days[i].courseMedicine = [];
            opt.days[i].supplyMedicine = [];
            opt.days[i].className = "";
            if( nowDate == opt.days[i].dealDate ) opt.days[i].className += " activing"; // 日期等于当前日期
            if( opt.expireDate && $bb.simple.dateDiff(opt.expireDate,opt.days[i].dealDate).days >= 0 ) opt.days[i].className += " disabled"; // 日期大于等于终止方案的日期
            if( opt.days[i].readOnly && opt.days[i].className.indexOf("disabled") == -1 ) opt.days[i].className += " disabled";
            for (var p = 0; p < opt.courseMedicine.length; p++) {
                for (var j = 0; j < opt.days[i].columns.length; j++) {
                    if( opt.courseMedicine[p].medicineId == opt.days[i].columns[j].medicineId ){
                        var obj = $.extend({},opt.days[i].columns[j]);
                        obj.planId = opt.courseMedicine[p].planId;
                        obj.medicineName = opt.courseMedicine[p].medicineName;
                        obj.medUnitName = opt.courseMedicine[p].medUnitName;
                        obj.frequencyTime = opt.courseMedicine[p].frequencyTime;
                        if( obj.deValue && obj.deValue != "" ){
                            if( obj.frequencyTime != 1 ){
                                var value = obj.deValue / obj.frequencyTime;
                                value = reg.test(value) ? value : value.toFixed(2);
                                obj.value = value + "*" + obj.frequencyTime;
                            }else{
                                obj.value = obj.deValue;
                            }
                        }
                        obj.className = '';
                        switch( opt.days[i].columns[j].dealStatus ){
                            case "00":
                                break;
                            case "01":
                                obj.className = "style-02";
                                break;
                            case "02":
                                obj.className = "style-03";
                                break;
                            case "03":
                                obj.className = "style-05";
                                break;
                            case "04":
                                obj.className = "style-04";
                                break;
                        }
                        opt.days[i].courseMedicine.push(obj);
                    }
                }
            }
            var cLen = opt.courseMedicine.length;
            var cdLen = opt.days[i].courseMedicine.length;
            if( cdLen < cLen ){
                for (var p = 0; p < opt.courseMedicine.length; p++) {
                    var flagA = false;
                    for (var j = 0; j < opt.days[i].courseMedicine.length; j++) {
                        if( opt.courseMedicine[p].medicineId == opt.days[i].courseMedicine[j].medicineId ){
                            flagA = true;
                        }
                    }
                    if( !flagA ){
                        var obj = {};
                        obj.status = opt.courseMedicine[p].status;
                        obj.planId = opt.courseMedicine[p].planId;
                        obj.medicineName = opt.courseMedicine[p].medicineName;
                        obj.medUnitName = opt.courseMedicine[p].medUnitName;
                        obj.frequencyTime = opt.courseMedicine[p].frequencyTime;
                        obj.medicineId = opt.courseMedicine[p].medicineId;
                        opt.days[i].courseMedicine.push(obj);
                    }
                }
            }
            for (var p = 0; p < opt.supplyMedicine.length; p++) {
                for (var j = 0; j < opt.days[i].columns.length; j++) {
                    if( opt.supplyMedicine[p].medicineId == opt.days[i].columns[j].medicineId ){
                        var obj = $.extend({},opt.days[i].columns[j]);
                        obj.status = opt.supplyMedicine[p].status;
                        obj.planId = opt.supplyMedicine[p].planId;
                        obj.medicineName = opt.supplyMedicine[p].medicineName;
                        obj.medUnitName = opt.supplyMedicine[p].medUnitName;
                        obj.frequencyTime = opt.supplyMedicine[p].frequencyTime;
                        if( obj.deValue && obj.deValue != "" ){
                            if( obj.frequencyTime != 1 ){
                                var value = obj.deValue / obj.frequencyTime;
                                value = reg.test(value) ? value : value.toFixed(2);
                                obj.value = value + "*" + obj.frequencyTime;
                            }else{
                                obj.value = obj.deValue;
                            }
                        }
                        obj.className = '';
                        switch( opt.days[i].columns[j].dealStatus ){
                            case "00":
                                break;
                            case "01":
                                obj.className = "style-02";
                                break;
                            case "02":
                                obj.className = "style-03";
                                break;
                            case "03":
                                obj.className = "style-05";
                                break;
                            case "04":
                                obj.className = "style-04";
                                break;
                        }
                        opt.days[i].supplyMedicine.push(obj);
                    }
                }
            }
            var m = opt.supplyMedicine.length;
            var n = opt.days[i].supplyMedicine.length;
            if( n < m ){
                for (var p = 0; p < opt.supplyMedicine.length; p++) {
                    var flagB = false;
                    for (var j = 0; j < opt.days[i].supplyMedicine.length; j++) {
                        if( opt.supplyMedicine[p].medicineId == opt.days[i].supplyMedicine[j].medicineId ){
                            flagB = true;
                        }
                    }
                    if( !flagB ){
                        var obj = {};
                        obj.status = opt.supplyMedicine[p].status;
                        obj.planId = opt.supplyMedicine[p].planId;
                        obj.medicineName = opt.supplyMedicine[p].medicineName;
                        obj.medUnitName = opt.supplyMedicine[p].medUnitName;
                        obj.frequencyTime = opt.supplyMedicine[p].frequencyTime;
                        obj.medicineId = opt.supplyMedicine[p].medicineId;
                        opt.days[i].supplyMedicine.push(obj);
                    }
                }
            }
            // 备注信息
            opt.days[i].important = false;
            for (var j = 0; j < opt.days[i].columns.length; j++) {
                if( opt.days[i].columns[j].deCode == $bb.code.getDeCodeByName("WBC") ) opt.days[i].WBC = opt.days[i].columns[j];
                if( opt.days[i].columns[j].deCode == $bb.code.getDeCodeByName("ANC") ) opt.days[i].ANC = opt.days[i].columns[j];
                if( opt.days[i].columns[j].deCode == $bb.code.getDeCodeByName("Hb") ) opt.days[i].Hb = opt.days[i].columns[j];
                if( opt.days[i].columns[j].deCode == $bb.code.getDeCodeByName("Plt") ) opt.days[i].Plt = opt.days[i].columns[j];
                if( opt.days[i].columns[j].deCode == $bb.code.getDeCodeByName("temperature") ) opt.days[i].temperature = opt.days[i].columns[j];
                if( opt.days[i].columns[j].deCode == $bb.code.getDeCodeByName("liverAndKidneyCheck") ) opt.days[i].liverAndKidneyCheck = opt.days[i].columns[j];
                if( opt.days[i].columns[j].deCode == $bb.code.getDeCodeByName("remark") ) opt.days[i].remark = opt.days[i].columns[j];
                if( opt.days[i].columns[j].deCode == $bb.code.getDeCodeByName("importantLevel") && opt.days[i].columns[j].deValue == "重要" ){
                    opt.days[i].className += " isImportant";
                }
            }
        }
        return opt;
    }

    /**
     * 设置开始时间
     */
    servers.ajaxSetStartTime = function(params,callback,arg) {
        $bb.ajax({
            url: URL + "specialistBag/medicine/initMedicine",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,arg.datas);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "设置开始时间出错！");
                    arg.dom.val(arg.oldValue);
                }
            }
        });
    }

    /**
     * 药品操作
     */
    servers.getDrugOperate = function(params){
        var list = [
            {"key":"01","text":"单项修改"},
            {"key":"02","text":"多项修改"},
            {"key":"03","text":"批量修改"},
            {"key":"04","text":"药物交换"}, // 暂时未做
            {"key":"05","text":"药物溯源"},
            {"key":"06","text":"用药记录"}
        ];
        var arr = [];
        for (var i = 0; i < params.length; i++) {
            arr.push(list[params[i]]);
        }
        return arr;
    }

    /**
     * 药品操作数据
     */
    servers.getDrugOperateData = function(oDiv,$dom){
        var list = [];
        var flagA = oDiv.hasClass("courseMedicine");
        if( oDiv.hasClass("activing") ){
            list = flagA ? [5,4] : [5];
        }else{
            var flag = false; // 初始化 选择的dom没有当前时间的
            $dom.each(function(i){
                if( $(this).hasClass("activing") ){
                    flag = true;
                }
            });
            if( flag ){
                list = flagA ? [0,4] : [0];
            }else{
                var time = oDiv.attr("data-dealdate");
                var flagB = $bb.simple.dateDiff(time,$bb.simple.getCurTime().date).days;
                list = flagA ? [0,2,4] : flagB > 0 ? [0,2,4] : [0,2];
            }
        }
        return list;
    }

    /**
     * 根据用药类型（正常用药，补充用药），获取对应的eventCode
     */
    servers.getEventCodeByType = function(oDiv){
        return oDiv.hasClass("courseMedicine") ? "C0002.0201.02" : "C0002.0502.01";
    }

    /**
     * 单项修改、多项修改、批量修改、用药记录请求接口时，参数中的source的获取
     */
    servers.getSourceByDealDate = function(dealDate){
        var nowDate = $bb.simple.getCurTime().date;
        return $bb.simple.dateDiff(nowDate,dealDate).days <= 0 ? "record" : "schedule";
    }

    /**
     * 单项修改、多项修改、批量修改、交换、用药记录
     */
    servers.ajaxChangeMedicineDose = function(params,callback,arg) {
        $bb.ajax({
            url: URL + "specialistBag/medicine/changeMedicineDose",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,arg);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "修改失败！");
                }
            }
        });
    }

    /**
     * 单项修改特殊值
     */
    servers.ajaxChangeSpecialValue = function (params, callback, arg) {
        $bb.ajax({
            url: URL + "specialistBag/other/changeOther",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,arg);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "修改失败！");
                }
            }
        })
    }

    /**
     * 溯源
     */
    servers.ajaxGetTraceInfo = function(params,callback,oDiv) {
        $bb.ajax({
            url: URL + "specialistBag/medicine/getTraceInfo",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,oDiv);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "查询溯源失败！");
                }
            }
        });
    }

    /**
     * 获取用药列表
     */
    servers.ajaxGetMedSkus = function(callback,datas) {
        $bb.ajax({
            url: URL + "medicineInfo/getMedSkusByClassId",
            data: JSON.stringify({
                data: {
                    "queryCriteria":{"medicineType":"2"} // 筛选补充用药
                }
            }),
            success: function(data) {
                callback(data,datas);
            }
        });
    }

    /**
     * 补充用药
     */
    servers.ajaxApplyMedicine = function(params,callback,arg){
        $bb.ajax({
            url: URL + "supplementMedicine/addMedicine",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,arg);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "补充用药应用失败！");
                }
            }
        });
    }

    /**
     * 获取药品信息
     */
    servers.ajaxGetMedInfo = function(params,callback,arg) {
        $bb.ajax({
            url: URL + "medicineInfo/getMedSkuInfo",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,arg);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "获取药品信息出错！");
                }
            }
        });
    }

    /**
     * 药品单位查询
     */
    servers.ajaxQueryUnit = function(callback,arg) {
        $bb.ajax({
            url: URL + "medUnitDict/queryAllMedUnitDict",
            success: function(data) {
                if (data.success) {
                    callback(data.data,arg);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "药品单位查询出错！");
                }
            }
        });
    }

    /**
     * 获取药品大类
     */
    servers.ajaxMedicineBigClass = function(callback,arg) {
        $bb.ajax({
            url: URL + "medicineClass/getAllMedicineClass",
            success: function(data) {
                if (data.success) {
                    callback(data.data,arg);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "获取药品大类出错！");
                }
            }
        });
    }

    /**
     * 获取药品小类
     */
    servers.ajaxMedicineSmallClass = function(params,callback,arg) {
        $bb.ajax({
            url: URL + "medicineInfo/getMedCatalogInfos",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,arg);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "获取药品小类出错！");
                }
            }
        });
    }

    /**
     * 修改药品
     */
    servers.ajaxUpdate = function(params,callback,arg) {
        $bb.ajax({
            url: URL + "medicineInfo/modifyMedSkuInfo",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,arg);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "修改药品出错！");
                }
            }
        });
    }

    /**
     * 添加药品
     */
    servers.ajaxAdd = function(params,callback,datas) {
        $bb.ajax({
            url: URL + "medicineInfo/insertMedSkuInfo",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,datas);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "添加药品出错！");
                }
            }
        });
    }

    /**
     * 终止用药
     */
    servers.ajaxStopMedicine = function(params,callback,arg) {
        $bb.ajax({
            url: URL + "supplementMedicine/stopMedicine",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,arg);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "终止用药出错！");
                }
            }
        });
    }

    /**
     * 终止方案
     */
    servers.ajaxStopScheme = function(params,callback,arg) {
        $bb.ajax({
            url: URL + "ptEhrCourse/stopScheme",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,arg);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "终止方案出错！");
                }
            }
        });
    }

    /**
     * 警示信息查询
     */
    servers.ajaxPtWarnMsg = function(params,callback,params2){
        $bb.ajax({
            url: URL + "remark/getPtWarnMsg",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,params2);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "警示信息查询出错！");
                }
            }
        });
    }

    /**
     * 查询专科袋备注模板
     */
    servers.ajaxReamrkJsonStr = function(params,callback) {
        $bb.ajax({
            url: URL + "remark/queryReamrkJsonStr",
            data: JSON.stringify({
                data: {
                    "code":params.code
                }
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,params);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "查询专科袋备注模板出错！");
                }
            }
        });
    }

    /**
     * 随访备注内容查询
     */
    servers.ajaxRemarkContent = function(arg,callback,params) {
        $bb.ajax({
            url: URL + "remark/listPtRemarkContent",
            data: JSON.stringify({
                data: arg
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,params);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "随访备注内容查询出错！");
                }
            }
        });
    }

    /**
     * 获取处理后可以匹配动态表单的value
     */
    servers.getValue = function(rows){
        var arr = [];
        for (var i = 0; i < rows.length; i++) {
            var obj = {};
            obj.quesValue = rows[i].deValue;
            obj.deCode = rows[i].deCode;
            obj.id = rows[i].id;
            arr.push(obj);
        }
        return arr;
    }

    /**
     * 获取处理后可以作为入参的deList
     */
    servers.getSaveValue = function(values,params){
        var arr = [];
        if( values.length > 0 ){
            for (var i = 0; i < values[0].valueList.length; i++) {
                var obj = {};
                obj.innDeCode = values[0].valueList[i].innDeCode;
                obj.deCode = values[0].valueList[i].deCode;
                obj.deValue = values[0].valueList[i].quesValue;
                obj.id = values[0].valueList[i].quesId;
                obj.dealDate = params.dealDate;
                obj.refDeValue = params.code;
                arr.push(obj);
            }
            for (var i = 0; i < arr.length; i++) {
                for (var j = 0; j < params.values.length; j++) {
                    if( arr[i].deCode == params.values[j].deCode ){
                        arr[i].id = params.values[j].id;
                    }
                }
            }
        }
        return arr;
    }

    /**
     * 修改备注信息
     */
    servers.ajaxInsertRemark = function(arg,callback,params) {
        $bb.ajax({
            url: URL + "remark/insertRemark",
            data: JSON.stringify({
                data: arg
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,params);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "修改备注信息出错！");
                }
            }
        });
    }

    /**
     * 根据药品名称查询
     */
    servers.ajaxSelect = function(params,callback,arg) {
        $bb.ajax({
            url: URL + "medicineInfo/queryMedSkuInfoByName",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,arg);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "根据药品名称查询药品出错！");
                }
            }
        });
    }

    /**
     * 添加一行
     */
    servers.ajaxAddDay = function(params,callback,datas){
        $bb.ajax({
            url: URL + "ptEhrCourse/addDay",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data,datas);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "添加一行出错！");
                }
            }
        });
    }

    /**
     * 获取当前方案的索引
     */
    servers.getSchemeIndex = function(schemes,currScheme){
        if( currScheme && typeof currScheme == "object" ){
            for (var i = 0; i < schemes.length; i++) {
                if( schemes[i].schemeId == currScheme.schemeId ){
                    return i;
                }
            }
            return 0;
        }else{
            return 0;
        }
    }

	return servers;
});